package com.tyl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tyl.ResultEntity;
import com.tyl.entity.AttrGroup;
import com.tyl.service.IAttrGroupService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 属性分组 前端控制器
 * </p>
 *
 * @author 汤云龙
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/Group")
public class AttrGroupController {
    @Resource
    private IAttrGroupService attrGroupService;
    @GetMapping("/selectGroupsByCategoryId/{categoryId}")
    public ResultEntity selectGroupsByCategoryId(@PathVariable("categoryId") Long categoryId ){
        //根据商品类别编号查询该类别下的属性分组信息
        List<AttrGroup> attrGroups = attrGroupService.selectByCategoryId(categoryId);
        return ResultEntity.ok(attrGroups);
    }
    @PostMapping("/add")
    public ResultEntity Add(@RequestBody AttrGroup attrGroup){
        //添加分组信息
        attrGroupService.save(attrGroup);
        return ResultEntity.ok("添加成功");
    }
    @GetMapping("/selectOne/{groupId}")
    public ResultEntity selectOne(@PathVariable("groupId") Long groupId ){
        AttrGroup attrGroup = attrGroupService.getById(groupId);
        return ResultEntity.ok(attrGroup);
    }
    @PutMapping("/update")
    public ResultEntity updateArrGroup(@RequestBody AttrGroup attrGroup){
        QueryWrapper<AttrGroup> attrGroupQueryWrapper = new QueryWrapper<>();
        attrGroupQueryWrapper.eq("attr_group_id",attrGroup.getAttrGroupId());
        attrGroupService.update(attrGroup, attrGroupQueryWrapper);
        return ResultEntity.ok("修改成功");
    }

}

